#!/bin/sh

action="$1"
config_file="$2"
config_nvram=".nvram"

decrypt()
{
        local config_tmp=".backup.cfg.all"

        base64 -d $config_file > $config_tmp
        sed -i '1d' $config_tmp
	cp -f $config_tmp $config_nvram

        rm -f $config_tmp
}

encrypt()
{
        local config_tmp=".backup.cfg.all"
	local config_tmp_md5=".backup.cfg.md5"

	md5sum $config_nvram | cut -d ' ' -f 1 > $config_tmp_md5
	cat $config_tmp_md5 $config_nvram > $config_tmp
	base64 $config_tmp > $config_file

        rm -f $config_tmp
}

usage()
{
	echo "usage:"
	echo "    1. decrypt config_file to $config_nvram file"
	echo "       $0 decrypt config_file"
	echo "    2. use nvram command to modify the $config_nvram file"
	echo "       ./nvram show"
	echo "       ./nvram set key=value"
	echo "    3. encrypt $config_nvram file to config_file"
	echo "       $0 encrypt config_file"	
}

case "$action" in
        "decrypt")
		decrypt
                ;;
        "encrypt")
		encrypt
                ;;
	*)
		usage		
		;;
esac
